package neusoft.example.com.yuejian06.Adapter;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

//import neusoft.example.com.yuejian06.Activity.MasterActivity;
import neusoft.example.com.yuejian06.Activity.MasterDetailActivity;
import neusoft.example.com.yuejian06.Activity.PictureDetailActivity;
import neusoft.example.com.yuejian06.Bean.Master;
import neusoft.example.com.yuejian06.PublicActivity;
import neusoft.example.com.yuejian06.R;

public class MasterListAdapter extends RecyclerView.Adapter{
    private PublicActivity publicActivity;
    private List<Master> masterList;

    public void setData(List<Master> list){
        masterList=list;
        notifyDataSetChanged();
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater=LayoutInflater.from(parent.getContext());
        View itemView=inflater.inflate(R.layout.master_item,parent,false);
        MasterListAdapter.ItemViewHolder viewHolder=new MasterListAdapter.ItemViewHolder(itemView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        publicActivity=new PublicActivity();
        final MasterListAdapter.ItemViewHolder viewHolder=(MasterListAdapter.ItemViewHolder)holder;
        final Master master=masterList.get(position);

        String master_name=master.getMaster_name();
        String master_nickname=master.getMaster_nickname();
        String master_intro=master.getMaster_intro();
        String master_pic=master.getMaster_pic();

        viewHolder.master_name.setText("大师："+master_name);
        viewHolder.master_nickname.setText("昵称："+master_nickname);
        viewHolder.master_intro.setText("简介："+master_intro);
        Picasso.with(viewHolder.imageView.getContext())
                .load(publicActivity.getIp()+"static/images/"+ master.getMaster_pic())
                .into(viewHolder.imageView);

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int pos=viewHolder.getLayoutPosition();
                Intent intent=new Intent(v.getContext(), MasterDetailActivity.class);
                intent.putExtra("masterID",masterList.get(pos).getMaster_id());
                v.getContext().startActivity(intent);
            }
        });


    }

    @Override
    public int getItemCount() {
        if(masterList!=null)
            return masterList.size();
        else
            return 0;
    }

    public class ItemViewHolder extends RecyclerView.ViewHolder {
        public ImageView imageView;
        public TextView master_name,master_nickname,master_intro;
        public View itemView;
        public ItemViewHolder(View itemView) {
            super(itemView);
            this.itemView=itemView;
            imageView=(ImageView)itemView.findViewById(R.id.pic);
            master_name=(TextView)itemView.findViewById(R.id.name);
            master_nickname=(TextView)itemView.findViewById(R.id.nickname);
            master_intro=(TextView)itemView.findViewById(R.id.intro);
        }
    }
}
